package com.jinsi.controller;

import com.jinsi.pojo.EquipCate;
import com.jinsi.pojo.TreeNode;
import com.jinsi.service.EquipcateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cate")
public class EquipcateController {


    @Autowired
    private EquipcateService equipcateService;

    @RequestMapping("/tree")
    public List<TreeNode>  getTree(){
        return equipcateService.getTree(0);
    }


    @RequestMapping("/edit")
    public Map<String,String> editTree(TreeNode treeNode){

        System.out.println(treeNode);
        Map<String,String> map = new HashMap<>();
        try {
            equipcateService.updateTreeName(treeNode);
            map.put("state","200");
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","500");
            map.put("msg","修改失败");

        }


        return map;
    }

    @RequestMapping("/del")
    public Map<String,String> delTree(int id){

        System.out.println(id);
        Map<String,String> map = new HashMap<>();
        try {
// 删除业务
            equipcateService.delTreeNode(id);
            map.put("state","200");
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","500");
            map.put("msg","删除失败");

        }


        return map;
    }

    @RequestMapping("/add")
    public Map<String,String> addTree(EquipCate equipCate){

        System.out.println(equipCate);
        Map<String,String> map = new HashMap<>();
        try {
// 添加业务
            equipcateService.addTreeNode(equipCate);

            map.put("state","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","500");
            map.put("msg","添加失败");

        }


        return map;
    }
}
